<?php

/*
 * use to view pages form database
 */

class PageViewer extends CAction {

    public $slug;
    public $page;

   

    private function createSideMenu() {


        $siblings = Tree::getSibling($this->page->sitemap->parent);
        $relatedPages = array();

        if ($siblings != null)
            foreach ($siblings as $spage) {
                $relatedPages[$spage->id] = array('label' => $spage->id0->title, 'url' => array($spage->id0->slug));
            }
           unset($relatedPages[$this->page->id]); 
        return $relatedPages;
    }

    public function render($view, array $options = array()) {
        $this->getController()->render($view, $options);
    }

    public function getSlug() {
//        $tragetUrl = explode(DIRECTORY_SEPARATOR, Yii::app()->request->getUrl());
        $pageURl= Yii::app()->request->getUrl();
        $siteUrl="/demo/bj2/";
//        $siteUrl="/bj2/";
//        var_dump($pageURl);
//        var_dump(substr($pageURl, strlen($siteUrl)));
//        exit();
        $this->slug = substr($pageURl, strlen($siteUrl));
        if ($this->slug == '')
            $this->slug = 'home';
        return $this->slug;
    }

    public function renderPage($page) {

        if ($this->slug == 'home')
            $this->render('index', array('page' => $page));
        else {
            $this->getController()->layout = '//layouts/column2';
            $this->getController()->breadcrumbs = $this->getController()->createBreadcrumbs();
            $this->getController()->sideMenu = $this->createSideMenu();
            $this->render('page', array('page' => $page));
        }
    }

    /**
     * Returns the page content requested by the user.

     * @return string the page requested by the user.

     */
    public function getRequestedPage($requestedSlug) {
//preg_match("/^\[autoload\]\[\w*\D*\w*\]/", $input_line, $output_array);
        try {
            
        

        $model = Page::model()->findByAttributes(
                array('slug' => $requestedSlug), 'published=1'
        );
        $this->page = $model;

        if($model != null){
          $output_array= Helper::matchStrip($this->page->content);
//        preg_match_all("^\[(.*?)\]^", $this->page->content, $output_array);
//        var_dump($output_array);
        if (isset($output_array[1][0]) && $output_array[1][0] == "autoload")
            $this->getController()->redirect($output_array[1][1]);
        }
        
//            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
        
        } catch (Exception $exc) {
             throw new CHttpException(500, 'Web server is unable to handle your request.');
            echo $exc->getTraceAsString();
        }
    }

    /**
     * Runs the action.
     * This method displays the page requested by the user.
     * @throws CHttpException if the page is invalid
     */
    public function run() {
        
        $page = $this->getRequestedPage($this->getSlug());

        if ($page === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        else {
            $this->renderPage($page);
        }
    }

}

?>
        